#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaRadioButton.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QButtonGroup>
#include <QDebug>

class ElaRadioButtonDemo : public ElaWidget
{
    Q_OBJECT

public:
    ElaRadioButtonDemo(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaRadioButton 功能演示");
        resize(600, 500);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | 
                            ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | 
                            ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        setupUI();
        setupConnections();
    }

private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 基础单选按钮展示
        mainLayout->addWidget(createBasicRadioGroup());

        // 2. 单选按钮组展示
        mainLayout->addWidget(createRadioButtonGroup());

        // 3. 布局方向展示
        mainLayout->addWidget(createLayoutDirectionGroup());

        // 4. 状态演示
        mainLayout->addWidget(createStateDemoGroup());

        // 5. 动态操作演示
        mainLayout->addWidget(createDynamicOperationGroup());

        mainLayout->addStretch();
    }

    // 创建基础单选按钮组
    QGroupBox *createBasicRadioGroup()
    {
        QGroupBox *group = new QGroupBox("基础单选按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示基本的单选按钮功能", this);
        layout->addWidget(infoLabel);

        // 单个单选按钮
        ElaRadioButton *radio1 = new ElaRadioButton("单个单选按钮", this);
        ElaRadioButton *radio2 = new ElaRadioButton("另一个单选按钮", this);
        ElaRadioButton *radio3 = new ElaRadioButton("第三个选项", this);

        layout->addWidget(radio1);
        layout->addWidget(radio2);
        layout->addWidget(radio3);

        m_basicRadios << radio1 << radio2 << radio3;

        return group;
    }

    // 创建单选按钮组（互斥选择）
    QGroupBox *createRadioButtonGroup()
    {
        QGroupBox *group = new QGroupBox("单选按钮组（互斥选择）", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("使用 QButtonGroup 实现互斥选择", this);
        layout->addWidget(infoLabel);

        // 创建按钮组确保互斥
        m_colorGroup = new QButtonGroup(this);
        m_colorGroup->setExclusive(true);

        // 颜色选择单选按钮
        ElaRadioButton *redRadio = new ElaRadioButton("红色", this);
        ElaRadioButton *greenRadio = new ElaRadioButton("绿色", this);
        ElaRadioButton *blueRadio = new ElaRadioButton("蓝色", this);
        ElaRadioButton *yellowRadio = new ElaRadioButton("黄色", this);

        // 添加到按钮组
        m_colorGroup->addButton(redRadio, 1);
        m_colorGroup->addButton(greenRadio, 2);
        m_colorGroup->addButton(blueRadio, 3);
        m_colorGroup->addButton(yellowRadio, 4);

        // 设置默认选择
        redRadio->setChecked(true);

        layout->addWidget(redRadio);
        layout->addWidget(greenRadio);
        layout->addWidget(blueRadio);
        layout->addWidget(yellowRadio);

        m_colorRadios << redRadio << greenRadio << blueRadio << yellowRadio;

        return group;
    }

    // 创建布局方向展示组
    QGroupBox *createLayoutDirectionGroup()
    {
        QGroupBox *group = new QGroupBox("布局方向展示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同布局方向下的单选按钮", this);
        layout->addWidget(infoLabel);

        // 水平布局
        QHBoxLayout *horizontalLayout = new QHBoxLayout();
        QLabel *horizontalLabel = new QLabel("水平布局:", this);
        
        ElaRadioButton *hRadio1 = new ElaRadioButton("选项A", this);
        ElaRadioButton *hRadio2 = new ElaRadioButton("选项B", this);
        ElaRadioButton *hRadio3 = new ElaRadioButton("选项C", this);

        horizontalLayout->addWidget(horizontalLabel);
        horizontalLayout->addWidget(hRadio1);
        horizontalLayout->addWidget(hRadio2);
        horizontalLayout->addWidget(hRadio3);
        horizontalLayout->addStretch();

        // 垂直布局
        QVBoxLayout *verticalLayout = new QVBoxLayout();
        QLabel *verticalLabel = new QLabel("垂直布局:", this);
        
        ElaRadioButton *vRadio1 = new ElaRadioButton("选项一", this);
        ElaRadioButton *vRadio2 = new ElaRadioButton("选项二", this);
        ElaRadioButton *vRadio3 = new ElaRadioButton("选项三", this);

        verticalLayout->addWidget(verticalLabel);
        verticalLayout->addWidget(vRadio1);
        verticalLayout->addWidget(vRadio2);
        verticalLayout->addWidget(vRadio3);

        layout->addLayout(horizontalLayout);
        layout->addLayout(verticalLayout);

        m_layoutRadios << hRadio1 << hRadio2 << hRadio3 << vRadio1 << vRadio2 << vRadio3;

        return group;
    }

    // 创建状态演示组
    QGroupBox *createStateDemoGroup()
    {
        QGroupBox *group = new QGroupBox("状态演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示单选按钮的不同状态", this);
        layout->addWidget(infoLabel);

        // 正常状态
        ElaRadioButton *normalRadio = new ElaRadioButton("正常状态（可选择）", this);
        normalRadio->setChecked(true);

        // 禁用状态
        ElaRadioButton *disabledRadio = new ElaRadioButton("禁用状态（不可选择）", this);
        disabledRadio->setEnabled(false);

        // 禁用但选中状态
        ElaRadioButton *disabledCheckedRadio = new ElaRadioButton("禁用但选中状态", this);
        disabledCheckedRadio->setChecked(true);
        disabledCheckedRadio->setEnabled(false);

        layout->addWidget(normalRadio);
        layout->addWidget(disabledRadio);
        layout->addWidget(disabledCheckedRadio);

        m_stateRadios << normalRadio << disabledRadio << disabledCheckedRadio;

        return group;
    }

    // 创建动态操作演示组
    QGroupBox *createDynamicOperationGroup()
    {
        QGroupBox *group = new QGroupBox("动态操作演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("演示单选按钮的动态操作", this);
        layout->addWidget(infoLabel);

        // 动态修改文本的单选按钮
        QHBoxLayout *dynamicTextLayout = new QHBoxLayout();
        m_dynamicTextRadio = new ElaRadioButton("原始文本", this);
        ElaRadioButton *changeTextBtn = new ElaRadioButton("点击我修改上面按钮文本", this);
        changeTextBtn->setCheckable(false); // 这个作为触发按钮，不参与单选
        
        dynamicTextLayout->addWidget(m_dynamicTextRadio);
        dynamicTextLayout->addWidget(changeTextBtn);
        dynamicTextLayout->addStretch();

        // 动态启用/禁用
        QHBoxLayout *enableDisableLayout = new QHBoxLayout();
        m_toggleEnableRadio = new ElaRadioButton("这个按钮会被启用/禁用", this);
        m_toggleEnableRadio->setChecked(true);
        ElaRadioButton *toggleEnableBtn = new ElaRadioButton("切换上面按钮的启用状态", this);
        toggleEnableBtn->setCheckable(false);
        
        enableDisableLayout->addWidget(m_toggleEnableRadio);
        enableDisableLayout->addWidget(toggleEnableBtn);
        enableDisableLayout->addStretch();

        // 选择状态显示
        QHBoxLayout *selectionLayout = new QHBoxLayout();
        m_selectionDisplay = new QLabel("当前选择: 无", this);
        
        ElaRadioButton *option1 = new ElaRadioButton("选项1", this);
        ElaRadioButton *option2 = new ElaRadioButton("选项2", this);
        ElaRadioButton *option3 = new ElaRadioButton("选项3", this);
        
        // 创建按钮组
        QButtonGroup *selectionGroup = new QButtonGroup(this);
        selectionGroup->addButton(option1);
        selectionGroup->addButton(option2);
        selectionGroup->addButton(option3);
        
        selectionLayout->addWidget(m_selectionDisplay);
        selectionLayout->addWidget(option1);
        selectionLayout->addWidget(option2);
        selectionLayout->addWidget(option3);
        selectionLayout->addStretch();

        layout->addLayout(dynamicTextLayout);
        layout->addLayout(enableDisableLayout);
        layout->addLayout(selectionLayout);

        m_dynamicRadios << m_dynamicTextRadio << m_toggleEnableRadio << option1 << option2 << option3;
        m_triggerButtons << changeTextBtn << toggleEnableBtn;
        m_selectionGroup = selectionGroup;

        return group;
    }

    void setupConnections()
    {
        // 颜色选择组信号
        connect(m_colorGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked),
                this, &ElaRadioButtonDemo::onColorSelected);

        // 动态文本修改
        connect(m_triggerButtons[0], &ElaRadioButton::clicked, this, [this]() {
            static int clickCount = 0;
            clickCount++;
            QString newText = QString("动态修改的文本 %1").arg(clickCount);
            m_dynamicTextRadio->setText(newText);
            qDebug() << "单选按钮文本已修改为:" << newText;
        });

        // 启用/禁用切换
        connect(m_triggerButtons[1], &ElaRadioButton::clicked, this, [this]() {
            bool currentState = m_toggleEnableRadio->isEnabled();
            m_toggleEnableRadio->setEnabled(!currentState);
            QString stateText = !currentState ? "已启用" : "已禁用";
            qDebug() << "单选按钮状态:" << stateText;
        });

        // 选择状态显示
        connect(m_selectionGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked),
                this, &ElaRadioButtonDemo::onSelectionChanged);

        // 基础单选按钮信号
        for (int i = 0; i < m_basicRadios.size(); ++i) {
            connect(m_basicRadios[i], &ElaRadioButton::toggled, this, [i, this](bool checked) {
                if (checked) {
                    qDebug() << "基础单选按钮" << i + 1 << "被选中:" << m_basicRadios[i]->text();
                }
            });
        }

        // 布局方向单选按钮信号
        for (ElaRadioButton *radio : m_layoutRadios) {
            connect(radio, &ElaRadioButton::toggled, this, [radio](bool checked) {
                if (checked) {
                    qDebug() << "布局单选按钮被选中:" << radio->text();
                }
            });
        }
    }

private Q_SLOTS:
    void onColorSelected(QAbstractButton *button)
    {
        if (button) {
            QString colorName = button->text();
            qDebug() << "选择的颜色:" << colorName;
            
            // 在实际应用中，这里可以触发相应的颜色变更操作
            // 例如：changeThemeColor(colorName);
        }
    }

    void onSelectionChanged(QAbstractButton *button)
    {
        if (button) {
            QString selection = button->text();
            m_selectionDisplay->setText(QString("当前选择: %1").arg(selection));
            qDebug() << "选择状态变更:" << selection;
        }
    }

private:
    // 基础单选按钮
    QList<ElaRadioButton*> m_basicRadios;
    
    // 颜色选择组
    QButtonGroup *m_colorGroup;
    QList<ElaRadioButton*> m_colorRadios;
    
    // 布局演示
    QList<ElaRadioButton*> m_layoutRadios;
    
    // 状态演示
    QList<ElaRadioButton*> m_stateRadios;
    
    // 动态操作
    QList<ElaRadioButton*> m_dynamicRadios;
    QList<ElaRadioButton*> m_triggerButtons;
    QButtonGroup *m_selectionGroup;
    
    // 动态操作控件指针
    ElaRadioButton *m_dynamicTextRadio;
    ElaRadioButton *m_toggleEnableRadio;
    QLabel *m_selectionDisplay;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaRadioButtonDemo demo;
    demo.moveToCenter();
    demo.show();

    return app.exec();
}

#include "ElaRadioButtonTest.moc"